package com.gwnet.shop.service.impl;

import com.gwnet.common.core.utils.DateUtils;
import com.gwnet.shop.domain.GoodsSpec;
import com.gwnet.shop.domain.ShopGoods;
import com.gwnet.shop.domain.SpecGroup;
import com.gwnet.shop.mapper.ShopGoodsMapper;
import com.gwnet.shop.service.IGoodsSpecService;
import com.gwnet.shop.service.IShopGoodsService;
import com.gwnet.shop.service.ISpecGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-19
 */
@Service
public class ShopGoodsServiceImpl implements IShopGoodsService
{
    @Resource
    private ShopGoodsMapper shopGoodsMapper;

    @Resource
    private IGoodsSpecService goodsSpecService;

    @Resource
    private ISpecGroupService specGroupService;

    /**
     * 查询商品信息
     * 
     * @param id 商品信息主键
     * @return 商品信息
     */
    @Override
    public ShopGoods selectShopGoodsById(Long id)
    {
        return shopGoodsMapper.selectShopGoodsById(id);
    }

    @Override
    public ShopGoods selectShopGoodsSpecById(Long id) {
        ShopGoods goods = shopGoodsMapper.selectShopGoodsById(id);
        //根据商品id查询对应的规格
        GoodsSpec goodsSpec = new GoodsSpec();
        goodsSpec.setGoodsId(goods.getId());
        List<GoodsSpec> specList = goodsSpecService.selectGoodsSpecList(goodsSpec);
        goods.setGoodsSpecs(specList);

        //根据商品id查询对应的规格group
        SpecGroup specGroup = new SpecGroup();
        specGroup.setGoodsId(goods.getId());
        List<SpecGroup> groupList = specGroupService.selectSpecGroupList(specGroup);
        goods.setSpecGroups(groupList);
        return goods;
    }

    /**
     * 查询商品信息列表
     * 
     * @param shopGoods 商品信息
     * @return 商品信息
     */
    @Override
    public List<ShopGoods> selectShopGoodsList(ShopGoods shopGoods)
    {
        return shopGoodsMapper.selectShopGoodsList(shopGoods);
    }

    /**
     * 新增商品信息
     * 
     * @param shopGoods 商品信息
     * @return 结果
     */
    @Override
    public int insertShopGoods(ShopGoods shopGoods)
    {
        shopGoods.setCreateTime(DateUtils.getNowDate());
        int rows = shopGoodsMapper.insertShopGoods(shopGoods);
        //添加规格
        //goodsSpecService.insertGoodsSpec(shopGoods.getGoodsSpecs(),shopGoods.getId());
        //添加规格分类及明细
        //specGroupService.insertSpecGroup(shopGoods.getSpecGroups(),shopGoods.getId());
        return rows;
    }

    /**
     * 修改商品信息
     * 
     * @param shopGoods 商品信息
     * @return 结果
     */
    @Override
    public int updateShopGoods(ShopGoods shopGoods)
    {
        shopGoods.setUpdateTime(DateUtils.getNowDate());
        return shopGoodsMapper.updateShopGoods(shopGoods);
    }

    /**
     * 批量删除商品信息
     * 
     * @param ids 需要删除的商品信息主键
     * @return 结果
     */
    @Override
    public int deleteShopGoodsByIds(Long[] ids)
    {
        return shopGoodsMapper.deleteShopGoodsByIds(ids);
    }

    /**
     * 删除商品信息信息
     * 
     * @param id 商品信息主键
     * @return 结果
     */
    @Override
    public int deleteShopGoodsById(Long id)
    {
        return shopGoodsMapper.deleteShopGoodsById(id);
    }
}
